home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / fileutil.13 / fileutil / fileutils-3.13 / src / system.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-03  |  8.6 KB  |  354 lines

  1. /* system-dependent definitions for fileutils programs.
  2.    Copyright (C) 89, 91, 92, 93, 94, 1996 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. /* Include sys/types.h before this file.  */
  19.  
  20. #include <sys/stat.h>
  21.  
  22. #ifdef STAT_MACROS_BROKEN
  23. #undef S_ISBLK
  24. #undef S_ISCHR
  25. #undef S_ISDIR
  26. #undef S_ISFIFO
  27. #undef S_ISLNK
  28. #undef S_ISMPB
  29. #undef S_ISMPC
  30. #undef S_ISNWK
  31. #undef S_ISREG
  32. #undef S_ISSOCK
  33. #endif /* STAT_MACROS_BROKEN.  */
  34.  
  35. #ifndef S_IFMT
  36. #define S_IFMT 0170000
  37. #endif
  38. #if !defined(S_ISBLK) && defined(S_IFBLK)
  39. #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
  40. #endif
  41. #if !defined(S_ISCHR) && defined(S_IFCHR)
  42. #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
  43. #endif
  44. #if !defined(S_ISDIR) && defined(S_IFDIR)
  45. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  46. #endif
  47. #if !defined(S_ISREG) && defined(S_IFREG)
  48. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  49. #endif
  50. #if !defined(S_ISFIFO) && defined(S_IFIFO)
  51. #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
  52. #endif
  53. #if !defined(S_ISLNK) && defined(S_IFLNK)
  54. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  55. #endif
  56. #if !defined(S_ISSOCK) && defined(S_IFSOCK)
  57. #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
  58. #endif
  59. #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
  60. #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
  61. #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
  62. #endif
  63. #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
  64. #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
  65. #endif
  66.  
  67. #ifndef S_IEXEC
  68. #define S_IEXEC S_IXUSR
  69. #endif
  70.  
  71. #ifdef S_IEXEC
  72. #ifndef S_IXUSR
  73. #define S_IXUSR S_IEXEC
  74. #endif
  75. #ifndef S_IXGRP
  76. #define S_IXGRP (S_IEXEC >> 3)
  77. #endif
  78. #ifndef S_IXOTH
  79. #define S_IXOTH (S_IEXEC >> 6)
  80. #endif
  81. #endif /* S_IEXEC */
  82.  
  83. #if !defined(HAVE_MKFIFO)
  84. #define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
  85. #endif
  86.  
  87. #ifdef HAVE_SYS_PARAM_H
  88. #include <sys/param.h>
  89. #endif
  90.  
  91. /* <unistd.h> should be included before any preprocessor test
  92.    of _POSIX_VERSION.  */
  93. #ifdef HAVE_UNISTD_H
  94. #include <unistd.h>
  95. #endif
  96.  
  97. #ifndef STDIN_FILENO
  98. #define STDIN_FILENO 0
  99. #endif
  100.  
  101. #ifndef STDOUT_FILENO
  102. #define STDOUT_FILENO 1
  103. #endif
  104.  
  105. #ifndef STDERR_FILENO
  106. #define STDERR_FILENO 2
  107. #endif
  108.  
  109. #include "pathmax.h"
  110.  
  111. /* FIXME: Don't use _POSIX_VERSION.  */
  112. #ifndef _POSIX_VERSION
  113. off_t lseek ();
  114. #endif
  115.  
  116. #ifdef TM_IN_SYS_TIME
  117. #include <sys/time.h>
  118. #else
  119. #include <time.h>
  120. #endif
  121.  
  122. /* Since major is a function on SVR4, we can't use `ifndef major'.  */
  123. #ifdef MAJOR_IN_MKDEV
  124. #include <sys/mkdev.h>
  125. #define HAVE_MAJOR
  126. #endif
  127. #ifdef MAJOR_IN_SYSMACROS
  128. #include <sys/sysmacros.h>
  129. #define HAVE_MAJOR
  130. #endif
  131. #ifdef major            /* Might be defined in sys/types.h.  */
  132. #define HAVE_MAJOR
  133. #endif
  134.  
  135. #ifndef HAVE_MAJOR
  136. #define major(dev)  (((dev) >> 8) & 0xff)
  137. #define minor(dev)  ((dev) & 0xff)
  138. #define makedev(maj, min)  (((maj) << 8) | (min))
  139. #endif
  140. #undef HAVE_MAJOR
  141.  
  142. #ifdef HAVE_UTIME_H
  143. #include <utime.h>
  144. #endif
  145.  
  146. /* Some systems (even some that do have <utime.h>) don't declare this
  147.    structure anywhere.  */
  148. #ifndef HAVE_STRUCT_UTIMBUF
  149. struct utimbuf
  150. {
  151.   long actime;
  152.   long modtime;
  153. };
  154. #endif
  155.  
  156. /* Don't use bcopy!  Use memmove if source and destination may overlap,
  157.    memcpy otherwise.  */
  158.  
  159. #ifdef HAVE_STRING_H
  160. # if !STDC_HEADERS && HAVE_MEMORY_H
  161. #  include <memory.h>
  162. # endif
  163. # include <string.h>
  164. #else
  165. # include <strings.h>
  166. char *memchr ();
  167. #endif
  168.  
  169. #include <errno.h>
  170. #ifndef errno
  171. extern int errno;
  172. #endif
  173.  
  174. #ifdef STDC_HEADERS
  175. #define getopt system_getopt
  176. #include <stdlib.h>
  177. #undef getopt
  178. #else /* not STDC_HEADERS */
  179. char *getenv ();
  180. #endif /* STDC_HEADERS */
  181.  
  182. /* The following test is to work around the gross typo in
  183.    systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
  184.    is defined to 0, not 1.  */
  185. #if !EXIT_FAILURE
  186. # undef EXIT_FAILURE
  187. # define EXIT_FAILURE 1
  188. #endif
  189.  
  190. #ifndef EXIT_SUCCESS
  191. # define EXIT_SUCCESS 0
  192. #endif
  193.  
  194. #ifdef HAVE_FCNTL_H
  195. #include <fcntl.h>
  196. #else
  197. #include <sys/file.h>
  198. #endif
  199.  
  200. #ifndef SEEK_SET
  201. #define SEEK_SET 0
  202. #define SEEK_CUR 1
  203. #define SEEK_END 2
  204. #endif
  205. #ifndef F_OK
  206. #define F_OK 0
  207. #define X_OK 1
  208. #define W_OK 2
  209. #define R_OK 4
  210. #endif
  211.  
  212. #ifdef HAVE_DIRENT_H
  213. # include <dirent.h>
  214. # define NLENGTH(direct) (strlen((direct)->d_name))
  215. #else /* not HAVE_DIRENT_H */
  216. # define dirent direct
  217. # define NLENGTH(direct) ((direct)->d_namlen)
  218. # ifdef HAVE_SYS_NDIR_H
  219. #  include <sys/ndir.h>
  220. # endif /* HAVE_SYS_NDIR_H */
  221. # ifdef HAVE_SYS_DIR_H
  222. #  include <sys/dir.h>
  223. # endif /* HAVE_SYS_DIR_H */
  224. # ifdef HAVE_NDIR_H
  225. #  include <ndir.h>
  226. # endif /* HAVE_NDIR_H */
  227. #endif /* HAVE_DIRENT_H */
  228.  
  229. #ifdef CLOSEDIR_VOID
  230. /* Fake a return value. */
  231. #define CLOSEDIR(d) (closedir (d), 0)
  232. #else
  233. #define CLOSEDIR(d) closedir (d)
  234. #endif
  235.  
  236. /* Get or fake the disk device blocksize.
  237.    Usually defined by sys/param.h (if at all).  */
  238. #ifndef DEV_BSIZE
  239. #ifdef BSIZE
  240. #define DEV_BSIZE BSIZE
  241. #else /* !BSIZE */
  242. #define DEV_BSIZE 4096
  243. #endif /* !BSIZE */
  244. #endif /* !DEV_BSIZE */
  245.  
  246. /* Extract or fake data from a `struct stat'.
  247.    ST_BLKSIZE: Optimal I/O blocksize for the file, in bytes.
  248.    ST_NBLOCKS: Number of 512-byte blocks in the file
  249.    (including indirect blocks). */
  250. #ifndef HAVE_ST_BLOCKS
  251. # define ST_BLKSIZE(statbuf) DEV_BSIZE
  252. # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE.  */
  253. #  define ST_NBLOCKS(statbuf) (((statbuf).st_size + 512 - 1) / 512)
  254. # else /* !_POSIX_SOURCE && BSIZE */
  255. #  define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
  256. # endif /* !_POSIX_SOURCE && BSIZE */
  257. #else /* HAVE_ST_BLOCKS */
  258. /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
  259. # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
  260.                    ? (statbuf).st_blksize : DEV_BSIZE)
  261. # if defined(hpux) || defined(__hpux__) || defined(__hpux)
  262. /* HP-UX counts st_blocks in 1024-byte units.
  263.    This loses when mixing HP-UX and BSD filesystems with NFS.  */
  264. #  define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * 2)
  265. # else /* !hpux */
  266. #  if defined(_AIX) && defined(_I386)
  267. /* AIX PS/2 counts st_blocks in 4K units.  */
  268. #    define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * 8)
  269. #  else /* not AIX PS/2 */
  270. #    define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
  271. #  endif /* not AIX PS/2 */
  272. # endif /* !hpux */
  273. #endif /* HAVE_ST_BLOCKS */
  274.  
  275. /* Convert B 512-byte blocks to kilobytes if K is nonzero,
  276.    otherwise return it unchanged. */
  277. #define convert_blocks(b, k) ((k) ? ((b) + 1) / 2 : (b))
  278.  
  279. #ifndef RETSIGTYPE
  280. #define RETSIGTYPE void
  281. #endif
  282.  
  283. #ifdef __GNUC__
  284. # undef alloca
  285. # define alloca __builtin_alloca
  286. #else
  287. # ifdef HAVE_ALLOCA_H
  288. #  include <alloca.h>
  289. # else
  290. #  ifndef _AIX
  291. /* AIX alloca decl has to be the first thing in the file, bletch! */
  292. char *alloca ();
  293. #  endif
  294. # endif
  295. #endif
  296.  
  297. #include <ctype.h>
  298.  
  299. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  300. #define IN_CTYPE_DOMAIN(c) 1
  301. #else
  302. #define IN_CTYPE_DOMAIN(c) isascii(c)
  303. #endif
  304.  
  305. #ifdef isblank
  306. #define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
  307. #else
  308. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  309. #endif
  310. #ifdef isgraph
  311. #define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
  312. #else
  313. #define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
  314. #endif
  315.  
  316. #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
  317. #define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
  318. #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
  319. #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
  320. #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
  321. #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
  322. #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
  323. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  324. #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
  325. #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
  326.  
  327. #ifndef __P
  328. #if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
  329. #define __P(args) args
  330. #else
  331. #define __P(args) ()
  332. #endif  /* GCC.  */
  333. #endif  /* Not __P.  */
  334.  
  335. /* Take care of NLS matters.  */
  336.  
  337. #if HAVE_LOCALE_H
  338. # include <locale.h>
  339. #endif
  340. #if !HAVE_SETLOCALE
  341. # define setlocale(Category, Locale) /* empty */
  342. #endif
  343.  
  344. #if ENABLE_NLS
  345. # include <libintl.h>
  346. # define _(Text) gettext (Text)
  347. #else
  348. # define bindtextdomain(Domain, Directory) /* empty */
  349. # define textdomain(Domain) /* empty */
  350. # define _(Text) Text
  351. #endif
  352.  
  353. #define STREQ(a,b) (strcmp((a), (b)) == 0)
  354.